package com.sunrise.gateway.web.servlet;

import com.sunrise.gateway.commons.CommonUtils;
import com.sunrise.gateway.commons.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetSessionIdServlet extends ServletBase {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doMyPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Session session = SecurityUtils.getSubject().getSession(false);
        Result result;
        if (session == null) {
            result = new Result(com.dap.commons.Constants.ResponseCode.SYS_SUCCESS, "", null);
        } else {
            final String sessionId = String.valueOf(session.getId());
            result = new Result(com.dap.commons.Constants.ResponseCode.SYS_SUCCESS, "", "\"" + sessionId + "\"");
        }
        CommonUtils.writeStrIntoResponse(result.toJsonString(), resp);
    }



    @Override
    protected void doMyGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doMyPost(req, resp);
    }


}
